package org.tity.sql.sql;

import static org.junit.Assert.assertTrue;

import org.junit.Test;
import org.tity.sql.GlobalSettings;
import org.tity.sql.dao.base.TableDataTest;
import org.tity.sql.sql.builder.Delete;
import org.tity.sql.sql.builder.Insert;
import org.tity.sql.sql.builder.SE;
import org.tity.sql.sql.builder.Select;

public class ExecutableSQLTest extends TableDataTest {
	
	@Test
	public void executeTest1()
	{
		//初始化数据
		int i=911;
		int z=dao.insert(normalTable).set("read_times", i%7==0?null:(56+70*Math.random())).set("price",i%5==0?null:(50+Math.random()*20)).set("id", i).execute();
		assertTrue(z==1);
		
		//通过设置DAO执行语句
		SE se1=new SE("update "+normalTable+" set read_times=? where id=?",110,911);
		se1.setDAO(dao);
		z=se1.execute();
		assertTrue(z==1);
		
		//打开全局设定
		GlobalSettings.USE_PRIMARY_DAO=true;
		
		//无需设定DAO，使用主DAO执行语句
		SE se2=new SE("select read_times from "+normalTable+" where id=?",911);
		int readTimes=se2.queryInteger();
		assertTrue(readTimes==110);
		
		//更加简洁的执行方式
		
		z=SE.get("update "+normalTable+" set read_times=? where id=?",1122,911).execute();
		assertTrue(z==1);
		
		readTimes=SE.get("select read_times from "+normalTable+" where id=?",911).queryInteger();
		assertTrue(readTimes==1122);
		
		Delete delete=new Delete(normalTable);
		z=delete.where("id=?", 911).execute();
		assertTrue(z==1);
		
	}
	
	@Test
	public void executeTest2()
	{
		//打开全局设定
		GlobalSettings.USE_PRIMARY_DAO=true;
		//初始化数据
		int i=911;
		Insert insert=new Insert(normalTable);
		int z=insert.set("read_times", 1122).set("price",i%5==0?null:(50+Math.random()*20)).set("id", i).execute();
		assertTrue(z==1);
		
		Select select=new Select(normalTable);
		select.select("read_times").where("id=?",911);
		int readTimes=select.queryInteger();
		assertTrue(readTimes==1122);
	}

}
